/**
 * Restful service for all service. It supports GET, PUT, POST and DELETE
 * operation, at the same time, the service will redirect to special location
 * when receive the error code of 403 and 404.
 * 
 * Use this as a template for all future
 * 
 * Use of Class.js
 * 
 * @author david liu
 */
var RestfulService = Class.extend({
	
	_get : function(url, successcb, errorcb) {
		httpPromise = this.$http.get(url);
		return this._thenFactoryMethod(httpPromise, successcb, errorcb);
	},
	
	_post : function(url, data, successcb, errorcb) {
		httpPromise = this.$http.post(url, data);
		return this._thenFactoryMethod(httpPromise, successcb, errorcb);
	},
	
	_put : function(url, data, successcb, errorcb) {
		httpPromise = this.$http.put(url, data);
		return this._thenFactoryMethod(httpPromise, successcb, errorcb);
	},
	
	_delete : function(url, successcb, errorcb) {
		httpPromise = this.$http.delete(url);
		return this._thenFactoryMethod(httpPromise, successcb, errorcb);
	},
	
	
	_thenFactoryMethod : function (httpPromise, successcb, errorcb) {
		var scb = successcb || angular.noop;
		var ecb = errorcb || angular.noop;
		
		
		return httpPromise.then(function(response) {
			var result = response.data;
			scb(result, response.status, response.headers, response.config);
			return response.data;
		}, function(response) {
			
			if(response.status === 403) {
				if(this.SharedVariableService && this.SharedVariableService['SessionId'] != undefined) {
					window.location =  this.SharedVariableService['timeout_base_url'] + '/' + this.SharedVariableService['SessionId'];
				} else {
					window.location =  SharedVariableService['login_url'];
				}
			} else if(response.status === 404) {
				window.location =  SharedVariableService['home_url'];
			} else {
				// alert('errr');
				ecb(undefined, response.status, response.headers, response.config);
			}
			
			return undefined;
		}.bind(this));
	}
});











